package cn.iocoder.yudao.module.enterprise.controller.admin.enterpriseJob;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.enterprise.controller.admin.enterpriseJob.vo.*;
import cn.iocoder.yudao.module.enterprise.convert.enterpriseJob.EnterpriseJobConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.enterpriseJob.EnterpriseJobDO;
import cn.iocoder.yudao.module.enterprise.service.enterpriseJob.EnterpriseJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -企业端- 招聘职位")
@RestController
@RequestMapping("/enterprise/enterprise-job")
@Validated
public class EnterpriseJobController {

    @Resource
    private EnterpriseJobService enterpriseJobService;

    @PostMapping("/create")
    @ApiOperation("创建招聘职位")
    public CommonResult<Long> createenterpriseJob(@Valid @RequestBody EnterpriseJobCreateReqVO createReqVO) {
        if(null==createReqVO.getTimingStatus()){
            createReqVO.setTimingStatus(0);
        }
        return success(enterpriseJobService.createJob(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新招聘职位")
    public CommonResult<Boolean> updateenterpriseJob(@Valid @RequestBody EnterpriseJobUpdateReqVO updateReqVO) {
        if(null==updateReqVO.getTimingStatus()){
            updateReqVO.setTimingStatus(0);
        }
        enterpriseJobService.updateJob(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除招聘职位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteJob(@RequestParam("id") Long id) {
        enterpriseJobService.deleteJob(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得招聘职位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<EnterpriseJobRespVO> getenterpriseJob(@RequestParam("id") Long id) {
        EnterpriseJobDO enterpriseJob = enterpriseJobService.getJob(id);
        return success(EnterpriseJobConvert.INSTANCE.convert(enterpriseJob));
    }
    @PutMapping("/updateEnterpriseJobFixed")
    @ApiOperation("更新职位定时暂停发布")
    public CommonResult<Boolean> updateEnterpriseJobFixed(@Valid @RequestBody EnterpriseJobUpdateVO updateReqVO) {
        if(updateReqVO.getTimingStatus()!=0 && StringUtils.isBlank(updateReqVO.getFixedTime())){
            return error("定时时间不能为空");
        }
        enterpriseJobService.updateEnterpriseJobFixed(updateReqVO.getId(),updateReqVO.getTimingStatus(),updateReqVO.getFixedTime());
        return success(true);
    }
    @GetMapping("/list")
    @ApiOperation("获得招聘职位列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<EnterpriseJobRespVO>> getenterpriseJobList(@RequestParam("ids") Collection<Long> ids) {
        List<EnterpriseJobDO> list = enterpriseJobService.getJobList(ids);
        return success(EnterpriseJobConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得招聘职位分页")
    public CommonResult<PageResult<EnterpriseJobRespVO>> getenterpriseJobPage(@Valid @RequestBody EnterpriseJobPageReqVO pageVO) {
        PageResult<EnterpriseJobDO> pageResult = enterpriseJobService.getJobPage(pageVO);
        return success(EnterpriseJobConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出招聘职位 Excel")
    @OperateLog(type = EXPORT)
    public void exportenterpriseJobExcel(@Valid EnterpriseJobExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<EnterpriseJobDO> list = enterpriseJobService.getJobList(exportReqVO);
        // 导出 Excel
        List<EnterpriseJobExcelVO> datas = EnterpriseJobConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "招聘职位.xls", "数据", EnterpriseJobExcelVO.class, datas);
    }

    @PostMapping("/import-excel")
    @ApiOperation("导入招聘职位 Excel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "Excel 文件", required = true, dataTypeClass = MultipartFile.class),
            @ApiImplicitParam(name = "updateSupport", value = "是否支持更新，默认为 false", example = "true", dataTypeClass = Boolean.class)
    })
    public CommonResult<JobImportRespVO> inputenterpriseJobExcel(@RequestParam("file") MultipartFile file,
                                        @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws IOException {
        // 导入 Excel
        List<EnterpriseJobImputReqVO> list=ExcelUtils.read(file,EnterpriseJobImputReqVO.class);
        return success(enterpriseJobService.importJob(list, updateSupport));
    }

    @PostMapping("/jobWaitExamineList")
    @ApiOperation("获得待发布招聘职位分页")
    public CommonResult<PageResult<EnterpriseJobRespVO>> getJobWaitExaminePage(@RequestBody EnterpriseJobPageReqVO pageVO) {
        PageResult<EnterpriseJobDO> pageResult = enterpriseJobService.getJobWaitExaminePage(pageVO, pageVO.getFlag());
        return success(EnterpriseJobConvert.INSTANCE.convertPage(pageResult));
    }

    @PutMapping("/examinJob")
    @ApiOperation("审核职位")
    public CommonResult<Boolean> examinJob(@RequestBody EnterpriseJobUpdateReqVO updateReqVO) {
        enterpriseJobService.examinJob(updateReqVO);
        return success(true);
    }

    @PostMapping("/jobExamineDoneList")
    @ApiOperation("获得已发布招聘职位分页")
    public CommonResult<PageResult<EnterpriseJobRespVO>> jobExamineDoneList(@RequestBody  EnterpriseJobPageReqVO pageVO) {
        PageResult<EnterpriseJobDO> pageResult = enterpriseJobService.getJobWaitExaminePage(pageVO, pageVO.getFlag());
        return success(EnterpriseJobConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/jobExamineStopList")
    @ApiOperation("获得暂停发布招聘职位分页")
    public CommonResult<PageResult<EnterpriseJobRespVO>> jobExamineStopList(@RequestBody EnterpriseJobPageReqVO pageVO) {
        PageResult<EnterpriseJobDO> pageResult = enterpriseJobService.getJobWaitExaminePage(pageVO, pageVO.getFlag());
        return success(EnterpriseJobConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/checkJobList")
    @ApiOperation("获得招聘职位列表分页")
    public CommonResult<PageResult<EnterpriseJobRespVO>> checkJobList(@RequestBody EnterpriseJobPageReqVO pageVO) {
        PageResult<EnterpriseJobDO> pageResult = enterpriseJobService.getJobPage(pageVO);
        return success(EnterpriseJobConvert.INSTANCE.convertPage(pageResult));
    }

    @PutMapping("/updateJobDate")
    @ApiOperation("刷新职位")
    public CommonResult<Boolean> updateJobDate(@RequestBody EnterpriseJobUpdateReqVO updateReqVO) {
        updateReqVO.setPublishTime(new Date());
        enterpriseJobService.updateJob(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateJobsDate")
    @ApiOperation("批量刷新职位")
    public CommonResult<Boolean> updateJobsDate(@RequestBody List<EnterpriseJobUpdateReqVO> updateReqVO) {
        enterpriseJobService.updateJobsDate(updateReqVO);
        return success(true);
    }


    @PutMapping("/setJobTopStatus")
    @ApiOperation("批量置顶/取消置顶职位")
    public CommonResult<Boolean> setJobsTopStatus(@RequestBody MemberJobidsVO memberJobidsVO) {
        enterpriseJobService.setJobsTopStatus(memberJobidsVO.getIds(), memberJobidsVO.getStatus());
        return success(true);
    }

    @PutMapping("/setJobStatus")
    @ApiOperation("批量修改职位状态")
    public CommonResult<Boolean> setJobStatus(@RequestBody MemberJobidsVO memberJobidsVO) {
        enterpriseJobService.setJobStatus(memberJobidsVO.getIds(),memberJobidsVO.getStatus());
        return success(true);
    }
    //todo 批量导入


    @DeleteMapping("/deleteJob")
    @ApiOperation("批量删除职位")
    public CommonResult<Boolean> deleteJob(@RequestBody MemberJobidsVO memberJobidsVO) {
        enterpriseJobService.deleteJob(memberJobidsVO.getIds());
        return success(true);
    }
    @GetMapping("/getEnterpriseJobCensus")
    @ApiOperation("企业-平台概览")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "type",value ="查询类型,0:默认(不传时间),1:时间间隔,2:本周,3:本月",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间(type==1时传)",dataType ="string" ),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间(type==1时传)",dataType ="string" ),
    })
    CommonResult<EnterpriseJobCensusRespVO> getEnterpriseJobCensus( Integer type,  String startDate,  String endDate){
        EnterpriseJobCensusRespVO j=enterpriseJobService.getEnterpriseJobCensus(NumberUtils.getInteger(type,0),startDate,endDate,null);
        return success(j);
    }


    @GetMapping("/getEnterpriseJobCensusById")
    @ApiOperation("企业-职位应聘统计")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "type",value ="查询类型,0:默认(不传时间),1:时间间隔,2:本周,3:本月",dataType ="int",required = true),
            @ApiImplicitParam(paramType = "query",name = "jobId",value ="职位ID",dataType ="long" ),
            @ApiImplicitParam(paramType = "query",name = "startDate",value ="开始时间(type==1时传)",dataType ="string" ),
            @ApiImplicitParam(paramType = "query",name = "endDate",value ="结束时间(type==1时传)",dataType ="string" ),
    })
    CommonResult<EnterpriseJobCensusRespVO> getEnterpriseJobCensusById( Integer type, Long jobId,  String startDate,  String endDate){
        EnterpriseJobCensusRespVO j=enterpriseJobService.getEnterpriseJobCensus(NumberUtils.getInteger(type,0),startDate,endDate,jobId);
        return success(j);
    }


    @PostMapping("/recoveryEnterpriseJobPage")
    @ApiOperation("企业-回收站(招聘职位)")
    public CommonResult<PageResult<EnterpriseJobRespVO>> recoveryEnterpriseJobPage(@RequestBody  EnterpriseJobPageReqVO pageVO) {
        PageResult<EnterpriseJobDO> pageResult = enterpriseJobService.recoveryEnterpriseJobPage(pageVO);
        return success(EnterpriseJobConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getRecoveryEnterpriseJobById")
    @ApiOperation("企业-回收站(根据ID查看)")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1", dataTypeClass = Long.class)
    public CommonResult<EnterpriseJobRespVO> getRecoveryEnterpriseJobById(@RequestParam("id") Long id) {
        EnterpriseJobDO memberJobDO = enterpriseJobService.getRecoveryMemberJobById(id);
        EnterpriseJobRespVO memberJobRespVO=null;
        if(null!=memberJobDO){
            memberJobRespVO=new EnterpriseJobRespVO();
            BeanUtils.copyProperties(memberJobDO,memberJobRespVO);
        }
        return success(memberJobRespVO);
    }
    @PutMapping("/updateBatchRecoveryEnterpriseJob")
    @ApiOperation("企业-回收站(批量还原)")
    public CommonResult<Boolean> updateBatchRecoveryEnterpriseJob( @RequestBody MemberJobidsVO memberJobidsVO) {
        if(CollectionUtils.isEmpty(memberJobidsVO.getIds())){
            return error("请先选择记录");
        }
        enterpriseJobService.updateBatchRecoveryEnterpriseJob(memberJobidsVO.getIds());
        return success(true);
    }
}
